package csci318.sfm.g3.controller;

import csci318.sfm.g3.fetest.Csci318;
import javafx.fxml.FXML;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import csci318.sfm.g3.fetest.Person;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.ObservableList;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.SelectionMode;

public class DatabaseController implements Initializable {

    @FXML
    private TableView<Person> tableView;
    @FXML
    private TextField userIDField;
    @FXML
    private TextField firstNameField;
    @FXML
    private TextField lastNameField;
    @FXML
    private TextField emailField;
    @FXML
    private Button saveButton;
    @FXML
    private Button cancelButton;
    @FXML
    private Button addButton;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        tableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
    }

    private void showTextField() {
        userIDField.setVisible(true);
        firstNameField.setVisible(true);
        lastNameField.setVisible(true);
        emailField.setVisible(true);
    }

    private void hideTextField() {
        userIDField.setVisible(false);
        firstNameField.setVisible(false);
        lastNameField.setVisible(false);
        emailField.setVisible(false);
    }

    private void resetTextField() {
        userIDField.setText("");
        firstNameField.setText("");
        lastNameField.setText("");
        emailField.setText("");
    }

    private void showButton(Button bt) {
        bt.setVisible(true);
    }

    private void hideButton(Button bt) {
        bt.setVisible(false);
    }

    @FXML
    private void addProcess() {
        showTextField();
        showButton(addButton);
        showButton(cancelButton);
    }

    @FXML
    private void deleteProcess() {
        ObservableList<Person> data = tableView.getItems();
        ObservableList<Integer> indices = tableView.getSelectionModel().getSelectedIndices();
        if (!indices.isEmpty()) {
            int[] sort = new int[indices.size()];
            for (int i = 0; i < indices.size(); i++) {
                sort[i] = indices.get(i).intValue();
            }
            for (int i = 0; i < sort.length; i++) {
                for (int j = i + 1; j < sort.length; j++) {
                    if (sort[j] > sort[i]) {
                        int tmp = sort[i];
                        sort[i] = sort[j];
                        sort[j] = tmp;
                    }
                }
            }
            for (int i = 0; i < sort.length; i++) {
                data.remove(sort[i]);
            }
        }
    }

    @FXML
    private void modifyProcess() {
        if (tableView.getSelectionModel().getSelectedIndex() >= 0) {
            showTextField();
            userIDField.setText(tableView.getSelectionModel().getSelectedItem().getUserID());
            firstNameField.setText(tableView.getSelectionModel().getSelectedItem().getFirstName());
            lastNameField.setText(tableView.getSelectionModel().getSelectedItem().getLastName());
            emailField.setText(tableView.getSelectionModel().getSelectedItem().getEmail());
            showButton(saveButton);
            showButton(cancelButton);
        }
    }

    @FXML
    private void clickAddButton() {
        ObservableList<Person> data = tableView.getItems();
        data.add(new Person(firstNameField.getText(),
                lastNameField.getText(),
                emailField.getText(),
                userIDField.getText()));
        resetTextField();
        hideTextField();
        hideButton(addButton);
        hideButton(cancelButton);
    }

    @FXML
    private void clickSaveButton() {
        tableView.getSelectionModel().getSelectedItem().setUserID(userIDField.getText());
        tableView.getSelectionModel().getSelectedItem().setFirstName(firstNameField.getText());
        tableView.getSelectionModel().getSelectedItem().setLastName(lastNameField.getText());
        tableView.getSelectionModel().getSelectedItem().setEmail(emailField.getText());
        resetTextField();
        hideTextField();
        hideButton(saveButton);
        hideButton(cancelButton);
    }

    @FXML
    private void clickCancelButton() {
        resetTextField();
        hideTextField();
        hideButton(saveButton);
        hideButton(addButton);
        hideButton(cancelButton);
    }

    @FXML
    private void backToSelect() {
        Csci318.getInstance().gotoAdminSelect();
    }
}
